package io.github.wppli.domain.authenticate.service.engine.impl;

import io.github.wppli.domain.authenticate.repository.IAuthenticateRepository;
import io.github.wppli.domain.authenticate.service.engine.IMessageLoader;
import io.github.wppli.email.domain.IEmailService;
import io.github.wppli.types.common.Validator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.stereotype.Service;

/**
 * @author li--jiaqiang 2024−12−23
 */
@Service("emailMessageLoader")
public class EmailMessageLoader implements IMessageLoader {

    private final IAuthenticateRepository userRepository;
    private final IEmailService emailService;
    public EmailMessageLoader(IAuthenticateRepository userRepository, IEmailService emailService) {
        this.userRepository = userRepository;
        this.emailService = emailService;
    }

    /**
     * 数字身份标识元素 - 邮箱 - 手机等
     */
    @Getter
    @AllArgsConstructor
    public enum DigitalIdentityEmailType {
        queryUserInfoWithEmail("queryUserInfoWithEmail"),
        unknownMethod("unknownMethod")
        ;
        // 执行器名字
        private final String actuatorName;
    }

    @Override
    public void sendCode(String code, String email) {
        boolean validEmail = Validator.isValidEmail(email);
        if (!validEmail) {
            throw new IllegalArgumentException("邮箱格式不正确");
        }
        emailService.sendText(email, "您的动态验证码为: " + code + " , 请勿泄露给他人, 五分钟内有效");
    }

    @Override
    public void setPassword(String email, String encodedPassword) {
        userRepository.setUserPasswordWithEmail(email, encodedPassword);
    }


}